package com.lp.user.controller;

import com.lp.model.config.AppResult;
import com.lp.model.config.CodeMsg;
import com.lp.model.dto.article.PageDto;
import com.lp.model.dto.user.FollowDto;
import com.lp.model.entity.article.vo.PageVO;
import com.lp.model.entity.user.User;
import com.lp.model.utils.PageUtils;
import com.lp.user.service.IRelationService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/relation")
public class RelationController {

    @Autowired
    private IRelationService relationService;

    /**
     * 粉丝列表
     */
    @PostMapping("/fans_list")
    public AppResult fansList(@RequestBody @NonNull PageDto page) {
        //1.非空校验
        if(page.getCurPage() == null || page.getPageSize() == null ||
                page.getId() == null) {
            return AppResult.fail(CodeMsg.FAIL_ERROR_ARTICLE_PAGE);
        }
        PageVO<User> fansList = relationService.getFansList(page);
        return AppResult.success(fansList);
    }

    /**
     * 关注列表
     */
    @PostMapping("/follow_list")
    public AppResult followList(@RequestBody @NonNull PageDto page) {
        //1.非法校验
        if(!PageUtils.checkPageDto(page)) {
            return AppResult.fail(CodeMsg.FAIL_ERROR_ARTICLE_PAGE);
        }
        PageVO<User> followList = relationService.getFollowList(page);
        return AppResult.success(followList);
    }

    /**
     * 关注/取关
     * @param followDto 关注者id 和 被关注者id
     */
    @PostMapping("/follow")
    public AppResult follow(@RequestBody @NonNull FollowDto followDto) {
        //1.非空校验
        if(followDto.getFollowedId() == null || followDto.getFollowerId() == null) {
            return AppResult.fail(CodeMsg.FAIL_NULL_FOLLOW);
        }
        //2.关注
        relationService.follow(followDto);
        return AppResult.success();
    }

}
